﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using DatabaseAccess;
using System.Text.RegularExpressions;
using System.Text;

namespace Liguo.Administration
{
    public partial class Goodsinfo : System.Web.UI.Page
    {
        static String str1 = "1";
        static String str2 = "0";
        static String id = "";
        //static String str2 = "asd";

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
             
                SqlIt c1 = new SqlIt();
                String ID = Request.QueryString["ID"];
                if (ID == null) { Response.Redirect("LoginFrame.aspx"); }
                DataSet ds = new DataSet();
                ds = c1.SelectAll("GoodsType");
                DropDownList1.DataSource = ds;
                DropDownList1.DataTextField = "TypeName";
                DropDownList1.DataValueField = "ID";
                DropDownList1.DataBind();
                SqlIt c2 = new SqlIt();
                DataSet ds1 = new DataSet();
                ds1 = c2.SelectAll("SaleMethod");
                DropDownList2.DataSource = ds1;
                DropDownList2.DataTextField = "Method";
                DropDownList2.DataValueField = "ID";
                DropDownList2.DataBind();
                DataSet a = new DataSet();
                SqlIt b = new SqlIt();
                a = b.SpecialItem("GoodsInfo", "ID="+ID, "*");
                id=a.Tables[0].Rows[0][0].ToString();
                TextBox1.Text = a.Tables[0].Rows[0][1].ToString();
                TextBox3.Text = a.Tables[0].Rows[0][3].ToString();
                TextBox4.Text = a.Tables[0].Rows[0][4].ToString();
                TextBox5.Text = a.Tables[0].Rows[0][5].ToString();
                TextBox7.Text = a.Tables[0].Rows[0][6].ToString();
                TextBox12.Text = a.Tables[0].Rows[0][7].ToString();
                TextBox13.Text = a.Tables[0].Rows[0][8].ToString();
                DropDownList2.Text = a.Tables[0].Rows[0][9].ToString();
                TextBox8.Text = a.Tables[0].Rows[0][11].ToString();
                TextBox9.Text = a.Tables[0].Rows[0][12].ToString();
                TextBox10.Text = a.Tables[0].Rows[0][13].ToString();
                TextBox11.Text = a.Tables[0].Rows[0][14].ToString();
                String temp = a.Tables[0].Rows[0][10].ToString();
                DropDownList1.Text = a.Tables[0].Rows[0][2].ToString();
                Image1.ImageUrl = ("~/images/" + temp + ".png");

            }
            

        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            
           
            TextBox1.ReadOnly =false;
            TextBox3.ReadOnly = false;
            TextBox4.ReadOnly = false;
            TextBox5.ReadOnly = false;
            TextBox7.ReadOnly = false;
            TextBox8.ReadOnly = false;
            TextBox9.ReadOnly = false;
            TextBox10.ReadOnly = false;
            TextBox11.ReadOnly = false;
            TextBox12.ReadOnly = false;
            TextBox13.ReadOnly = false;
            Button3.Visible = true;
            Button1.Visible = false;
            
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            Response.Redirect("GoodsType.aspx");

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            TextBox1.ReadOnly = true;
            TextBox3.ReadOnly = true;
            TextBox4.ReadOnly = true;
            TextBox5.ReadOnly = true;
            TextBox7.ReadOnly = true;
            TextBox8.ReadOnly = true;
            TextBox9.ReadOnly = true;
            TextBox10.ReadOnly = true;
            TextBox11.ReadOnly = true;
            TextBox12.ReadOnly = true;
            TextBox13.ReadOnly = true;
            Button3.Visible = false;
            Button1.Visible = true;
            LoadPage();
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            str1 = DropDownList1.SelectedValue;

        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            str2 = DropDownList2.SelectedValue;
        }

        protected void Button3_Click1(object sender, EventArgs e)
        {
            if (TextBox1.Text.Length > 20 || TextBox3.Text.Length > 30 || TextBox4.Text.Length > 7 || TextBox8.Text.Length > 40|| TextBox9.Text.Length > 20 || TextBox10.Text.Length > 5 || TextBox11.Text.Length > 5)
            {
                Response.Write("<script>alert('输入出错')</script>");
                return;
            }
            if (TextBox1.Text.Length==0 || TextBox4.Text.Length==0|| TextBox9.Text.Length==0 || TextBox10.Text.Length ==0|| TextBox11.Text.Length==0)
            {
                Response.Write("<script>alert('请输入数据')</script>");
                return;
            }
            if (!Regex.IsMatch(TextBox4.Text, "^([0-9]{1,}[.][0-9]*)$"))
            {

                Response.Write("<script>alert('请输入浮点数字')</script>");
                return;
            }

            if (!Regex.IsMatch(TextBox11.Text, @"^-?\d+$"))
            {

                Response.Write("<script>alert('请输入数字')</script>");
                return;
            }
            SqlIt c1 = new SqlIt();
            String Goodsname = TextBox1.Text;

            String Tags = TextBox3.Text;
            String DPrice = TextBox4.Text;
            String str1 = DropDownList1.Text;

            String Introduction = TextBox8.Text;
            String Manufacturers = TextBox9.Text;
            String GuaranteePeriod = TextBox10.Text;
            String Weight = TextBox11.Text;
            Dictionary<string, string> myDic = new Dictionary<string, string>();

            myDic["Goodsname"] = Goodsname;
            myDic["Tags"] = Tags;
            myDic["Price"] = DPrice;
            myDic["Type"] = str1;

            myDic["SaleMethod"] = str2;
            myDic["Introduction"] = Introduction;
            myDic["Manufacturers"] = Manufacturers;
            myDic["GuaranteePeriod"] = GuaranteePeriod;
            myDic["Weight"] = Weight;
            c1.UpdateMess("GoodsInfo", myDic, "ID='" + id + "'");
            Response.Write("<script type='text/javascript'>alert('成功！');</script>");
            TextBox1.ReadOnly = true;
            TextBox3.ReadOnly = true;
            TextBox4.ReadOnly = true;
            TextBox5.ReadOnly = true;
            TextBox7.ReadOnly = true;
            TextBox8.ReadOnly = true;
            TextBox9.ReadOnly = true;
            TextBox10.ReadOnly = true;
            TextBox11.ReadOnly = true;
            TextBox12.ReadOnly = true;
            TextBox13.ReadOnly = true;
            Button3.Visible = false;
            Button1.Visible=true;
            LoadPage();
        }
        private void LoadPage()
        {
            SqlIt c1 = new SqlIt();
            String ID = Request.QueryString["ID"];
            DataSet ds = new DataSet();
            ds = c1.SelectAll("GoodsType");
            DropDownList1.DataSource = ds;
            DropDownList1.DataTextField = "TypeName";
            DropDownList1.DataValueField = "ID";
            DropDownList1.DataBind();
            SqlIt c2 = new SqlIt();
            DataSet ds1 = new DataSet();
            ds1 = c2.SelectAll("SaleMethod");
            DropDownList2.DataSource = ds1;
            DropDownList2.DataTextField = "Method";
            DropDownList2.DataValueField = "ID";
            DataSet a = new DataSet();
            SqlIt b = new SqlIt();
            a = b.SpecialItem("GoodsInfo", "ID="+ID, "*");
            TextBox1.Text = a.Tables[0].Rows[0][1].ToString();
            TextBox3.Text = a.Tables[0].Rows[0][3].ToString();
            TextBox4.Text = a.Tables[0].Rows[0][4].ToString();
            TextBox5.Text = a.Tables[0].Rows[0][5].ToString();
            TextBox7.Text = a.Tables[0].Rows[0][6].ToString();
            TextBox12.Text = a.Tables[0].Rows[0][7].ToString();
            TextBox13.Text = a.Tables[0].Rows[0][8].ToString();
            DropDownList2.Text = a.Tables[0].Rows[0][9].ToString();
            TextBox8.Text = a.Tables[0].Rows[0][11].ToString();
            TextBox9.Text = a.Tables[0].Rows[0][12].ToString();
            TextBox10.Text = a.Tables[0].Rows[0][13].ToString();
            TextBox11.Text = a.Tables[0].Rows[0][14].ToString();
            String temp = a.Tables[0].Rows[0][10].ToString();
            Image1.ImageUrl = ("~/images/" + temp + ".png");
            DropDownList1.Text = a.Tables[0].Rows[0][2].ToString();
        }
    }
}
